﻿using System;
using System.Data.SqlTypes;
using System.Text;
using Microsoft.SqlServer.Server;

[Serializable]
[Microsoft.SqlServer.Server.SqlUserDefinedAggregate(Format.UserDefined, IsInvariantToDuplicates = false, IsInvariantToNulls = true, IsInvariantToOrder = false, IsNullIfEmpty = true, MaxByteSize = 8000)]
public struct MergeLine : IBinarySerialize
{
    private StringBuilder _StringBuilder;
    public void Init() => _StringBuilder = new StringBuilder();
    public void Accumulate(SqlString Value)
    {
        if (Value.IsNull) return;
        _StringBuilder.AppendLine(Value.Value);
    }
    public void Merge(MergeLine Group)
    {
        _StringBuilder.AppendLine(Group._StringBuilder.ToString());
    }
    public SqlString Terminate()
    {
        return new SqlString(_StringBuilder.ToString());
    }
    #region IBinarySerialize Members 
    public void Read(System.IO.BinaryReader r) => _StringBuilder = new StringBuilder(r.ReadString());
    public void Write(System.IO.BinaryWriter w) => w.Write(_StringBuilder.ToString());
    #endregion 
}